package com.zuikaku.task;


import com.zuikaku.tool.ZuikakuTool;

import java.io.File;
import java.util.*;
import java.util.concurrent.Callable;

/**
 * @author nanoda
 * @date 2021/12/6
 * @note
 */
public class FixExcelTask implements Callable<List<String>> {

    /**
     * 输入文件夹（不以/结尾）
     */
    private String inputFolder;

    /**
     * 输出文件夹（不以/结尾）
     */
    private String outputFolder;

    public FixExcelTask(String inputFolder, String outputFolder) {
        this.inputFolder = inputFolder;
        this.outputFolder = outputFolder;
    }

    public FixExcelTask() {
    }

    public String getInputFolder() {
        return inputFolder;
    }

    public void setInputFolder(String inputFolder) {
        this.inputFolder = inputFolder;
    }

    public String getOutputFolder() {
        return outputFolder;
    }

    public void setOutputFolder(String outputFolder) {
        this.outputFolder = outputFolder;
    }

    @Override
    public List<String> call() throws Exception {
        Date startTime =new Date();
        System.out.println("任务开始："+ZuikakuTool.formatDate(startTime));
        List<File> fileList = ZuikakuTool.getExcelFileListRecursive(new File(inputFolder));
        System.out.println("共扫描到："+fileList.size()+"个excel文件");
        List<String> failFilePathList = new ArrayList<>();
        //构建相对路径Map
        HashMap<String,File> fileHashMap = new HashMap<>();
        fileList.forEach(p->{
            String relativePath = p.getPath().substring(inputFolder.length());
            fileHashMap.put(relativePath,p);

        });
        Set<Map.Entry<String,File>> entrySet = fileHashMap.entrySet();
        int counter =0;
        for (Map.Entry<String, File> p : entrySet) {
            File targetFile = new File(outputFolder + p.getKey());
            boolean isOk = ZuikakuTool.fixExcel(p.getValue(), targetFile);
            if (isOk) {
                counter++;
                System.out.println("成功：" + p.getValue().getPath());

            } else {
                System.err.println("失败：" + p.getValue().getPath());
            }
        }
        Date endTime =new Date();
        int useTime =(int)((endTime.getTime()-startTime.getTime())/1000);
        System.out.println("任务结束：共处理"+counter+"个excel文件，耗时："+useTime+"s");
        return failFilePathList;

    }
}
